package br.com.transporte.controller;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import br.com.transporte.modelo.Motorista;
import br.com.transporte.tabelas.TabelaMotorista;
import br.com.transporte.app.Principal;
import br.com.transporte.dao.MotoristaDAO;
import javafx.beans.property.IntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class MotoristaController implements Initializable{

	@FXML
    private TableView<TabelaMotorista> tabelaMot;

    @FXML
    private TextField txCNH;

    @FXML
    private TableColumn clNome;

    @FXML
    private TextField txCodigo;

    @FXML
    private Button btSalvar;

    @FXML
    private TextField txBusca;

    @FXML
    private Button btnEditar;

    @FXML
    private TextField txNome;

    @FXML
    private TextField txCPF;

    @FXML
    private TableColumn clCNH;

    @FXML
    private Button btLimpar;

    @FXML
    private TableColumn clCPF;

    @FXML
    private TableColumn clCodigo;

    @FXML
    private Button btnExcluir;
    
    @FXML
    private Button btnVeiculo;

    @FXML
    void eventSalvar(ActionEvent event) {
    	Motorista m = new Motorista();
    	m.setNome(txNome.getText());
    	m.setCNH(txCNH.getText());
    	m.setCpf(txCPF.getText());
    	MotoristaDAO dao = new MotoristaDAO();
    	
    	if(!txCodigo.getText().trim().equals("")){
    		m.setCodigo(Integer.parseInt(txCodigo.getText()));
    		dao.Editar(m);
    	}else{
    		dao.Inserir(m);    		
    	}
		carregarTabela();
    }

    @FXML
    void eventLimpar(ActionEvent event) {
    	txCodigo.setText("");
    	txCNH.setText("");
    	txCPF.setText("");
    	txNome.setText("");
    }

    @FXML
    void eventEditar(ActionEvent event) {
    	IntegerProperty cdMotorista = tabelaMot.getSelectionModel().getSelectedItem().codigoProperty();
    	
    	MotoristaDAO dao = new MotoristaDAO();
    	Motorista mSalvo = dao.listarPorId(cdMotorista.intValue());
    	txCodigo.setText(String.valueOf(mSalvo.getCodigo()));
    	txCNH.setText(String.valueOf(mSalvo.getCNH()));
    	txNome.setText(mSalvo.getNome());
    	txCPF.setText(mSalvo.getCpf());
    }

    @FXML
    void eventExcluir(ActionEvent event) {
    	int linhaSelecionada = tabelaMot.getSelectionModel().getSelectedIndex();
    	IntegerProperty cdMotorista = tabelaMot.getSelectionModel().getSelectedItem().codigoProperty();
    	MotoristaDAO dao = new MotoristaDAO();
    	dao.Excluir(cdMotorista.intValue());
    	tabelaMot.getItems().remove(linhaSelecionada);
    }

    @FXML
    void eventPesquisar(ActionEvent event) {
    	MotoristaDAO dao = new MotoristaDAO();
		List<Motorista> motoristas = dao.listarNome(txBusca.getText());
		ObservableList<TabelaMotorista> dados = FXCollections.observableArrayList();
		
		for(Motorista m: motoristas){
			dados.add(new TabelaMotorista(m));
		}
		
		clCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
		clNome.setCellValueFactory(new PropertyValueFactory("nome"));
		clCNH.setCellValueFactory(new PropertyValueFactory("cnh"));
		clCPF.setCellValueFactory(new PropertyValueFactory("cpf"));
		tabelaMot.setItems(dados);
    }

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		carregarTabela();
	}
	
	private void carregarTabela() {
		MotoristaDAO dao = new MotoristaDAO();
		List<Motorista> motoristas = dao.listar();
		ObservableList<TabelaMotorista> dados = FXCollections.observableArrayList();
		
		for(Motorista m: motoristas){
			dados.add(new TabelaMotorista(m));
		}
		
		clCodigo.setCellValueFactory(new PropertyValueFactory("codigo"));
		clNome.setCellValueFactory(new PropertyValueFactory("nome"));
		clCNH.setCellValueFactory(new PropertyValueFactory("cnh"));
		clCPF.setCellValueFactory(new PropertyValueFactory("cpf"));
		tabelaMot.setItems(dados);
	}
	
	@FXML
    void eventVeiculo(ActionEvent event) {
		try{
			Parent root = FXMLLoader.load(getClass().getResource("/br/com/transporte/view/Veiculo.fxml"));
			Principal.scene.setRoot(root);
		} catch(IOException ex){
			ex.printStackTrace();
		}
    }
}
